package com.example.myapplication;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.core.app.NotificationCompat;

public class ReminderReceiver extends BroadcastReceiver {
    private static final String CHANNEL_ID = "ReminderChannel";
    private static final String CHANNEL_NAME = "提醒通知";
    private static final int NOTIFICATION_ID = 1;

    @Override
    public void onReceive(Context context, Intent intent) {
        String title = intent.getStringExtra("title");
        String message = intent.getStringExtra("message");
        String type = intent.getStringExtra("type"); // "assignment" or "exam"

        // 创建通知渠道（适配 Android 8.0+）
        createNotificationChannel(context);

        // 设置点击通知跳转的 Intent
        Intent targetIntent;
        // 打印收到广播
        System.out.println("Received a reminder: " + title + " - " + message);
        if ("assignment".equals(type)) {
            targetIntent = new Intent(context, ReviewFragment.class);
        } else {
            targetIntent = new Intent(context, TransactionFragment.class);
        }
        // 创建Dialog 作为通知
        if (false) {
            Intent dialogIntent = new Intent(context, ReminderDialogActivity.class);
            dialogIntent.putExtra("title", title);
            dialogIntent.putExtra("message", message);
            dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 必须设置此标志以启动 Activity
            context.startActivity(dialogIntent);
            return;
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // 构建通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle(title)
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);

        // 显示通知
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }

    private void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    CHANNEL_NAME,
                    NotificationManager.IMPORTANCE_HIGH
            );
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
    }
}
